package com.dycloud.project.controller;

import com.dycloud.project.aspectj.lang.annotation.Log;
import com.dycloud.project.aspectj.lang.enums.BusinessType;
import com.dycloud.project.domain.Course;
import com.dycloud.project.request.ReviewCourseRequest;
import com.dycloud.project.request.UpdateIsActiveRequest;
import com.dycloud.project.service.ICourseService;
import com.dycloud.project.web.controller.BaseController;
import com.dycloud.project.web.domain.AjaxResult;
import com.dycloud.project.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author : renhang
 * @description CourseController
 * @date : 2025-07-02 21:52
 **/

@RestController
@RequestMapping("/business/course")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService courseService;

    /**
     * 查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course) {
        startPage();
        return courseService.selectCourseList(course);
    }


    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('business:course:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course) {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('business:course:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('business:course:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(courseService.deleteCourseByIds(ids));
    }


    /**
     * 管理员审核
     *
     * @param request request
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasRole('admin')")
    @Log(title = "课程审核", businessType = BusinessType.REVIEW)
    @PutMapping("review")
    public AjaxResult review(@RequestBody @Validated ReviewCourseRequest request) {
        courseService.review(request);
        return AjaxResult.success();
    }


    /**
     * 修改课程开设状态
     *
     * @param request request
     * @return: @return {@link AjaxResult }
     */

    @PreAuthorize("@ss.hasPermi('business:course:updateIsActive')")
    @Log(title = "课程开设状态", businessType = BusinessType.UPDATE)
    @PutMapping("updateIsActive")
    public AjaxResult updateIsActive(@RequestBody @Validated UpdateIsActiveRequest request) {
        courseService.updateIsActive(request.getId(), request.getIsActive());
        return AjaxResult.success();
    }

}